if(SERVER) then
	AddCSLuaFile("shared.lua")
end

if(CLIENT) then
	SWEP.Slot = 0
	SWEP.SlotPos = 7
	SWEP.DrawAmmo = false
	SWEP.DrawCrosshair = false
	SWEP.IconLetter    = "n"
	SWEP.IconFont		= "HL2SelectIcons"
	killicon.AddFont("stunstick", "HL2MPTypeDeath", "!", Color(100, 100, 100, 255))
	surface.CreateFont("csd", ScreenScale(30), 500, true, true, "CSKillIcons")  
	surface.CreateFont("HL2MP", ScreenScale(30), 500, true, true, "HL2MPTypeDeath")  
	surface.CreateFont("csd", ScreenScale(60), 500, true, true, "CSSelectIcons")  
	surface.CreateFont("HalfLife2", ScreenScale(60), 500, true, true, "HL2SelectIcons")  
end

// Variables that are used on both client and server
SWEP.PrintName 		= "Stunstick"
SWEP.Instructions	= "Left click to stun more, with less damage\n Right click to beat the crap out of them, with less stun effect"
SWEP.Contact		= ""
SWEP.Purpose		= ""

SWEP.ViewModelFOV	= 62
SWEP.ViewModelFlip	= false
SWEP.AnimPrefix		= "stunstick"

SWEP.Spawnable			= false
SWEP.AdminSpawnable		= true

SWEP.NextStrike = 0
  
SWEP.ViewModel = Model("models/weapons/v_stunstick.mdl")
SWEP.WorldModel = Model("models/weapons/w_stunbaton.mdl")
  
SWEP.Sound = Sound("weapons/stunstick/stunstick_swing1.wav")
  
SWEP.Primary.ClipSize		= -1					// Size of a clip
SWEP.Primary.DefaultClip	= 0				// Default number of bullets in a clip
SWEP.Primary.Automatic		= false				// Automatic/Semi Auto
SWEP.Primary.Ammo			= ""

SWEP.Secondary.ClipSize		= -1				// Size of a clip
SWEP.Secondary.DefaultClip	= 0				// Default number of bullets in a clip
SWEP.Secondary.Automatic	= false				// Automatic/Semi Auto
SWEP.Secondary.Ammo			= ""

/*---------------------------------------------------------
   Name: SWEP:Initialize()
   Desc: Called when the weapon is first loaded
---------------------------------------------------------*/
function SWEP:Initialize()

	self:SetWeaponHoldType("melee")

	self.Hit = { 
	Sound("weapons/stunstick/stunstick_impact1.wav"),
  	Sound("weapons/stunstick/stunstick_impact2.wav") }
	
	self.FleshHit = {
  	Sound("weapons/stunstick/stunstick_fleshhit1.wav"),
  	Sound("weapons/stunstick/stunstick_fleshhit2.wav") }

end


/*---------------------------------------------------------
   Name: SWEP:PrimaryAttack()
   Desc: +attack1 has been pressed
---------------------------------------------------------*/
function SWEP:PrimaryAttack()

	if(CurTime() < self.NextStrike) then return end
	self.Owner:SetAnimation(PLAYER_ATTACK1)
	self.Weapon:EmitSound(self.Sound)
	self.Weapon:SendWeaponAnim(ACT_VM_HITCENTER)

	self.NextStrike = (CurTime() + .3)

	self.Owner:LagCompensation(true)
	local trace = self.Owner:GetEyeTrace()
	self.Owner:LagCompensation(false)

	if(not ValidEntity(trace.Entity)) then
		return
	end

	if(self.Owner:EyePos():Distance(trace.Entity:GetPos()) > 120) then
		return
	end
		
	local   data = EffectData()  
	data:SetNormal(trace.HitNormal)  
	data:SetOrigin(trace.HitPos + (trace.HitNormal * 4.0)) 
	util.Effect("StunstickImpact", data)  

	if(SERVER) then 
		if(trace.Entity:IsPlayer()) then
			local stunamount = 50
			trace.Entity:ViewPunch(Angle(stunamount*((math.random()*4)-2), stunamount*((math.random()*4)-2), stunamount*((math.random()*2)-1)))
			StunPlayer(trace.Entity, stunamount)
			self.Owner:EmitSound(self.FleshHit[math.random(1,#self.FleshHit)])
			trace.Entity:TakeDamage(10, self.Owner, self.Weapon)
		else
			self.Owner:EmitSound(self.Hit[math.random(1,#self.Hit)])
			trace.Entity:TakeDamage(25, self.Owner, self.Weapon)
		end	
	end
end
 
function SWEP:SecondaryAttack()
  
end 

function SWEP:DrawWeaponSelection(x, y, wide, tall, alpha)
	draw.SimpleText(self.IconLetter, self.IconFont, x + wide/2, y + tall*0.2, Color(255, 210, 0, 255), TEXT_ALIGN_CENTER)
end